function New-Build
{
    $version    = "1.0.3"
    $msbuild    = "$($env:frameworkdir)v4.0.30319\msbuild.exe"
    $basedir    = resolve-path .
    $sourcedir  = "$basedir\Library"
    $prjfile    = "$sourcedir\Microsoft.Services.TestTools.UITesting.Html.csproj"
    $config     = "debug"
    $builddir   = "$basedir\build"
    $pkgdir     = "$builddir\package"
    $toolsdir   = "$pkgdir\tools"
    $contentdir = "$pkgdir\content"
    $libdir     = "$pkgdir\lib\net40"
    $nuget      = "$basedir\.nuget\nuget.exe"

    if(!(test-path $nuget)){
        throw "
            Couldn't find nuget.exe ($nuget). 
            Download nuget.exe from http://nuget.codeplex.com/releases/view/58939
        "
    }

    if(!(test-path $msbuild)){
        throw "Couldn't find msbuild.exe ($msbuild)"
    }

    if(test-path $builddir){
        rm $builddir -recurse -force
    }

    Generate-AssemblyInfo `
        -clscompliant   "false" `
        -title          "CodedUI.CodeFirst" `
        -description    "Code First API Library, Scaffolding and Guidance for Coded UI Tests" `
        -company        "Microsoft" `
        -product        "CodedUI.CodeFirst" `
        -version        $version `
        -copyright      "Copyright © Microsoft 2012" `
        -file           "$sourcedir\Properties\AssemblyInfo.cs" 

    &"$msbuild" "$prjfile" /p:Configuration=$config  "/p:OutDir=$builddir\$config\"

    mkdir $pkgdir               > $null
    mkdir $toolsdir             > $null
    mkdir $libdir               > $null
    mkdir $contentdir           > $null
    mkdir "$toolsdir\TestCase"  > $null

    cp $builddir\$config\Microsoft.Services.TestTools.UITesting.Html.dll $libdir -force
    cp $sourcedir\CodeTemplates\Scaffolders\CodedUI.CodeFirst.psm1 $toolsdir -force
    cp $sourcedir\CodeTemplates\Scaffolders\init.ps1 $toolsdir -force
    cp $sourcedir\CodeTemplates\Scaffolders\install.ps1 $toolsdir -force
    cp $sourcedir\CodeTemplates\Scaffolders\InstallationDummyFile.txt $contentdir -force
    cp $sourcedir\CodeTemplates\Scaffolders\TestCase\*.* $toolsdir\TestCase -force
    cp $sourcedir\CodeTemplates\Scaffolders\CodedUI.CodeFirst.nuspec $pkgdir -force

    &"$nuget" pack "$pkgdir\CodedUI.CodeFirst.nuspec" -Version $version -OutputDirectory $builddir

    Generate-ApiDocs `
        -xml "$builddir\$config\Microsoft.Services.TestTools.UITesting.Html.xml" `
        -xsl "$basedir\docs\apireference.xsl" `
        -out "$builddir\apireference.wiki.html"
}

function Generate-AssemblyInfo {
    param(
        [string]$clscompliant,
        [string]$title, 
        [string]$description, 
        [string]$company, 
        [string]$product, 
        [string]$copyright, 
        [string]$version,
        [string]$file
    )
  
  $changeset = Get-HgChangeset
  $assemblyInfo = "using System;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: CLSCompliantAttribute($clscompliant)]
[assembly: ComVisibleAttribute(false)]
[assembly: AssemblyTitleAttribute(""$title"")]
[assembly: AssemblyDescriptionAttribute(""$description"")]
[assembly: AssemblyCompanyAttribute(""$company"")]
[assembly: AssemblyProductAttribute(""$product"")]
[assembly: AssemblyCopyrightAttribute(""$copyright"")]
[assembly: AssemblyDelaySignAttribute(false)]
[assembly: AssemblyVersionAttribute(""$version"")]
[assembly: AssemblyFileVersionAttribute(""$version"")]
[assembly: AssemblyInformationalVersionAttribute(""$version / $changeset"")]"

    out-file -filePath $file -encoding utf8 -inputObject $assemblyInfo
}

function Get-HgChangeset {
    $changeset = hg -q tip
    $changeset.split(':')[1]
}

function Generate-ApiDocs {
    
    param(
        [string]$xml, 
        [string]$xsl,
        [string]$out
    )

    $xpathdoc = new-object -type "System.Xml.XPath.XPathDocument" -argumentlist $xml
    $xslt = new-object -type "System.Xml.Xsl.XslCompiledTransform"
    $xslt.Load($xsl)
    $writer = new-object -type "System.Xml.XmlTextWriter" -argumentlist $out,$null
    $xslt.Transform(
        [System.Xml.XPath.XPathDocument]$xpathdoc, 
        [System.Xml.Xsl.XsltArgumentList]$null,
        [System.Xml.XmlTextWriter]$writer
        )

}

New-Build